﻿Imports System.Windows.Forms
Imports Abyx.SVCMS.Library
Imports Abyx.SVCMS.Library.DB
Imports Abyx.WindowsExtendedControls

Public Class SVCM_Form
    Implements ICarfParentWindow


#Region "Main Window Functionality"

#Region "Main Form"
    Private Sub SVCM_Form_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load

        LoadLicenseInformation()

        InitializeApplicationInterface()

        AppSettings.LoadSettings()

        InitializeThisFormAndControlSettings(False)

    End Sub
    Private Sub SVCM_Form_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        AppSettings.SaveSettings()
    End Sub
#End Region

#Region "Banner Tab"
    Private Sub Banner_StylishTabControl_Resize(sender As Object, e As System.EventArgs) Handles Banner_StylishTabControl.Resize
        LicenseTabControlsResizeRelocate()
    End Sub
    Private Sub Banner_StylishTabControl_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles Banner_StylishTabControl.SelectedIndexChanged
        Select Case Banner_StylishTabControl.SelectedIndex
            Case Is = PanelContentTypesEnum.Service
                PanelContentsRefreshContentType(PanelContentTypesEnum.Service)
            Case Is = PanelContentTypesEnum.Catalog
                PanelContentsRefreshContentType(PanelContentTypesEnum.Catalog)
            Case Is = PanelContentTypesEnum.SparePartsRequests
                PanelContentsRefreshContentType(PanelContentTypesEnum.SparePartsRequests)
            Case Is = PanelContentTypesEnum.Security
                PanelContentsRefreshContentType(PanelContentTypesEnum.Security)
            Case Is = PanelContentTypesEnum.System
                PanelContentsRefreshContentType(PanelContentTypesEnum.System)
            Case Else
                PanelContentsRefreshContentType(PanelContentTypesEnum.Home)
        End Select
    End Sub
#Region "   Buttons"
#Region "       Service"
    Private Sub Service_NewSO_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Service_NewSO_ImageButton.Click
        MNU_Service_NewServiceOrder()
    End Sub
    Private Sub Service_Customers_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Service_Customers_ImageButton.Click
        MNU_Service_Customers()
    End Sub
#End Region
#Region "       Catalog Tab"
    Private Sub Catalog_AddEquipment_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Catalog_AddEquipment_ImageButton.Click
        MNU_Catalog_AddEquipment()
    End Sub
    Private Sub Catalog_EditEquipment_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Catalog_EditEquipment_ImageButton.Click
        If Me.iPanelContentsType = PanelContentTypesEnum.Catalog Then
            If Me.LeftSidePanel_DetailListBox.SelectedItem IsNot Nothing Then
                PanelContentsItemOpen_Catalog(Me.LeftSidePanel_DetailListBox.SelectedItem)
            Else
                Me.StatusInfoMsgPut("No catalog items selected for alteration")
            End If
        End If
    End Sub
    Private Sub Catalog_CloneEquipment_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Catalog_CloneEquipment_ImageButton.Click
        If Me.iPanelContentsType = PanelContentTypesEnum.Catalog Then
            If Me.LeftSidePanel_DetailListBox.SelectedItem IsNot Nothing Then
                Dim lItem As DetailListBoxItem = Me.LeftSidePanel_DetailListBox.SelectedItem
                MNU_Catalog_CloneEquipment(lItem.DataObject)
            Else
                Me.StatusInfoMsgPut("No catalog items selected for cloning")
            End If
        End If
    End Sub
    Private Sub Catalog_DeleteEquipment_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Catalog_DeleteEquipment_ImageButton.Click
        If Me.iPanelContentsType = PanelContentTypesEnum.Catalog Then
            If Me.LeftSidePanel_DetailListBox.SelectedItem IsNot Nothing Then
                Dim item As DetailListBoxItem = Me.LeftSidePanel_DetailListBox.SelectedItem
                MNU_Catalog_DeleteEquipment(item.DataObject)
            Else
                Me.StatusInfoMsgPut("No equipment selected for deletion")
            End If
        End If
    End Sub
    Private Sub Catalog_Manufacturers_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Catalog_Manufacturers_ImageButton.Click
        MNU_Catalog_Manufacturers()
    End Sub
#End Region
#Region "       Spare Parts"
    Private Sub SpareParts_BulkStockEntry_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles SpareParts_BulkStockEntry_ImageButton.Click
        MNU_SpareParts_BulkStockEntry()
    End Sub
    Private Sub SpareParts_InventoryBatches_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles SpareParts_InventoryBatches_ImageButton.Click
        MNU_SpareParts_InventoryBatches()
    End Sub
    Private Sub SpareParts_NewGRN_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles SpareParts_NewGRN_ImageButton.Click
        MNU_SpareParts_NewGRN()
    End Sub
    Private Sub SpareParts_ConfigureCatalogSpares_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles SpareParts_ConfigureCatalogSpares_ImageButton.Click
        MNU_SpareParts_ConfigureCatalogSpares()
    End Sub
#End Region
#Region "       Security Tab"
    Private Sub Security_AddUser_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Security_AddUser_ImageButton.Click
        MNU_Security_AddUser()
    End Sub
    Private Sub Security_AlterUser_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Security_AlterUser_ImageButton.Click
        If Me.iPanelContentsType = PanelContentTypesEnum.Security Then
            If Me.LeftSidePanel_DetailListBox.SelectedItem IsNot Nothing Then
                PanelContentsItemOpen_Security(Me.LeftSidePanel_DetailListBox.SelectedItem)
            Else
                Me.StatusInfoMsgPut("No users selected for alteration")
            End If
        End If
    End Sub
    Private Sub Security_DeleteUser_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Security_DeleteUser_ImageButton.Click
        If Me.iPanelContentsType = PanelContentTypesEnum.Security Then
            If Me.LeftSidePanel_DetailListBox.SelectedItem IsNot Nothing Then
                Dim item As DetailListBoxItem = Me.LeftSidePanel_DetailListBox.SelectedItem
                MNU_Security_DeleteUser(item.DataObject)
            Else
                Me.StatusInfoMsgPut("No users selected for deletion")
            End If
        End If
    End Sub
    Private Sub Security_ChangePassword_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Security_ChangePassword_ImageButton.Click
        MNU_Security_ChangePassword()
    End Sub
    Private Sub Security_ResetAdminUser_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Security_ResetAdminUser_ImageButton.Click
        MNU_Security_ResetAdminUser()
    End Sub
    Private Sub Security_SetAdminPassword_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Security_SetAdminPassword_ImageButton.Click
        MNU_Security_SetAdminPassword()
    End Sub
#End Region
#Region "       System Tab"
    Private Sub System_ConfiguewDB_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles System_ConfiguewDB_ImageButton.Click
        MNU_System_ConfigureDB()
    End Sub
    Private Sub DbBackup_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles DbBackup_ImageButton.Click
        MNU_System_DbBackup()
    End Sub
#End Region
#End Region
#End Region

#Region "Left Side Panel"
    Private leftSidePanelIsExpanded As Boolean = True
    Private iPanelContentsType As PanelContentTypesEnum = PanelContentTypesEnum.Home
    Enum PanelContentTypesEnum As Integer
        Home = 0
        Service = 1
        Catalog = 2
        SparePartsRequests = 3
        Security = 4
        System = 5
    End Enum
    Private Sub LeftSideBar_VerticalLabel_Click(sender As Object, e As System.EventArgs) Handles LeftSideBar_VerticalLabel.Click
        LeftSidePanelToggleExpand()
    End Sub
    Private Sub LeftSidePanelToggleExpand()
        If Me.leftSidePanelIsExpanded Then
            'Deflate
            Me.leftSidePanelIsExpanded = False
            Me.LeftSideBar_Splitter.Visible = False
            Me.LeftSideBar_Panel.Visible = False
        Else
            'Expand
            Me.leftSidePanelIsExpanded = True
            Me.LeftSideBar_Splitter.Visible = True
            Me.LeftSideBar_Panel.Visible = True
        End If
    End Sub
    Private Sub LeftSidePanelSearch_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles LeftSidePanelSearch_ImageButton.Click
        PanelContentsRefreshExisting()
    End Sub
    Private Sub PanelContentsRefreshContentType(ContentType As PanelContentTypesEnum)
        Me.iPanelContentsType = ContentType
        Me.PanelContentsRefreshExisting()
    End Sub
    Private Sub PanelContentsRefreshExisting()
        Me.LeftSidePanel_DetailListBox.Items.Clear()
        Select Case Me.iPanelContentsType
            Case Is = PanelContentTypesEnum.Catalog
                PanelContentsRefresh_Catalog()
            Case Is = PanelContentTypesEnum.SparePartsRequests
                PanelContentsRefresh_SparePartsRequests()
            Case Is = PanelContentTypesEnum.Security
                PanelContentsRefresh_Users()
            Case Else
                PanelContentsRefresh_Service()
        End Select
        If (LeftSidePanel_DetailListBox.Items.Count > 0) Then
            If (Me.leftSidePanelIsExpanded = False) Then
                LeftSidePanelToggleExpand()
            End If
        Else
            If (Me.leftSidePanelIsExpanded) Then
                LeftSidePanelToggleExpand()
            End If
        End If
    End Sub
    Private Sub PanelContentsRefresh_Service()

        'Set title
        LeftSidePanel_ContentType_Label.Text = "Service Orders"

        'Every one is authorized to this menu
        'Fillup list 
        Dim svcMan As New SvcManager(AppSettings.LoggedUser)
        Dim sos As DataTable = svcMan.Get_SODetails_DT(LeftSidePanelSearch_TextBox.Text)
        LeftSidePanel_DetailListBox.Items.Clear()
        If sos.Rows.Count > 0 Then
            For i As Integer = 0 To sos.Rows.Count - 1
                Dim row As DataRow = sos.Rows(i)
                Dim header, details, footer As String
                header = row("OrderNumber") & "-" & row("CustomerNo") & " " & row("Name")
                If (IsDBNull(row("Mob")) = False) AndAlso _
                    (row("Mob") <> "") Then
                    header &= " " & row("Mob")
                End If
                details = row("details")
                footer = row("OrderReceivedDate")
                LeftSidePanel_DetailListBox.Items.Add(New DetailListBoxItem(header, details, footer, Nothing, row("ServiceOrderId")))
            Next
        End If

    End Sub
    Private Sub PanelContentsRefresh_Catalog()

        'Set title
        LeftSidePanel_ContentType_Label.Text = "Equipment Catalog"

        'Every one is authorized to this menu
        'Fillup list 
        Dim sqcMan As New SqcManager(AppSettings.LoggedUser)
        Dim catalog As CatalogItemTypeCollection = sqcMan.Get_CatalogItems_All()
        For Each catItem As CatalogItemType In catalog
            If LeftSidePanelSearch_TextBox.Text <> "" Then
                Dim sText As String = catItem.ProductModelNumber & " " & catItem.Name & " " & catItem.Description
                sText = sText.ToLower
                If sText.Contains(LeftSidePanelSearch_TextBox.Text.ToLower) Then
                    LeftSidePanel_DetailListBox.Items.Add(New DetailListBoxItem(catItem.Name, catItem.Description, catItem.ProductModelNumber, catItem.Picture, catItem))
                End If
            Else
                LeftSidePanel_DetailListBox.Items.Add(New DetailListBoxItem(catItem.Name, catItem.Description, catItem.ProductModelNumber, catItem.Picture, catItem))
            End If
        Next


    End Sub
    Private Sub PanelContentsRefresh_SparePartsRequests()

        'Set title
        LeftSidePanel_ContentType_Label.Text = "Spare Parts Requests"

        'Every one is authorized to this menu
        'Fillup list 



    End Sub
    Private Sub PanelContentsRefresh_Users()

        'Set title
        LeftSidePanel_ContentType_Label.Text = "Login Users"

        'Check authorization
        Select Case AppSettings.LoggedUser.AccessLevel
            Case Is = SEC_AccessLevelsEnum.System_Administrator, SEC_AccessLevelsEnum.Manager

                'Fillup list
                Dim uMan As New SecManager(AppSettings.LoggedUser)
                Dim users As UserTypeCollection = uMan.Get_UsersInfos_All()
                For Each user As UserType In users
                    If LeftSidePanelSearch_TextBox.Text <> "" Then
                        Dim sText As String = user.UserName & " " & user.UserType & " " & SysManager.DateFormatDisplay(user.LMDate)
                        sText = sText.ToLower
                        If sText.Contains(LeftSidePanelSearch_TextBox.Text.ToLower) Then
                            LeftSidePanel_DetailListBox.Items.Add(New DetailListBoxItem(user.UserName, user.AccessLevel.ToString, user.LMDate, Nothing, user))
                        End If
                    Else
                        LeftSidePanel_DetailListBox.Items.Add(New DetailListBoxItem(user.UserName, user.AccessLevel.ToString, user.LMDate, Nothing, user))
                    End If
                Next

            Case Else
                Me.StatusInfoMsgPut("You are not authorized to view login users, and hence content list will be disabled")
                Exit Sub
        End Select

    End Sub
    Private Sub LeftSidePanel_DetailListBox_DoubleClick(sender As Object, e As System.EventArgs) Handles LeftSidePanel_DetailListBox.DoubleClick
        If LeftSidePanel_DetailListBox.SelectedItem IsNot Nothing Then
            Select Case Me.iPanelContentsType
                Case Is = PanelContentTypesEnum.Home
                    PanelContentsItemOpen_Service(LeftSidePanel_DetailListBox.SelectedItem)
                Case Is = PanelContentTypesEnum.Service
                    PanelContentsItemOpen_Service(LeftSidePanel_DetailListBox.SelectedItem)
                Case Is = PanelContentTypesEnum.Catalog
                    PanelContentsItemOpen_Catalog(LeftSidePanel_DetailListBox.SelectedItem)
                Case Is = PanelContentTypesEnum.SparePartsRequests
                Case Is = PanelContentTypesEnum.Security
                    PanelContentsItemOpen_Security(LeftSidePanel_DetailListBox.SelectedItem)
                Case Is = PanelContentTypesEnum.System
            End Select
        End If
    End Sub
    Private Sub PanelContentsItemOpen_Security(Item As DetailListBoxItem)
        If Item.DataObject IsNot Nothing Then
            MNU_Security_AlterUser(Item.DataObject)
        End If
    End Sub
    Private Sub PanelContentsItemOpen_Catalog(Item As DetailListBoxItem)
        If Item.DataObject IsNot Nothing Then
            MNU_Catalog_AlterEquipment(Item.DataObject)
        End If
    End Sub
    Private Sub PanelContentsItemOpen_Service(Item As DetailListBoxItem)
        If Item.DataObject IsNot Nothing Then
            MNU_Service_AlterSO(Item.DataObject)
        End If
    End Sub

#End Region

#End Region

#Region "Private Procedures"

#Region "General"
    Private Sub InitializeApplicationInterface()

        RefreshLoginInformation()

        InitializeStatusInfoMsgBar()

        'CheckLisenseInformation()

        PanelContentsRefreshExisting()

        EnableDisableAllowedAndDisallowedFunctions()

    End Sub
#End Region

#Region "Service"
    Private Sub MNU_Service_NewServiceOrder()

        Dim frm As New SVC_ServiceOrder_Form
        frm.MdiParent = Me
        frm.Show(FormActionOnDataEnum.NewRecord, Nothing)

    End Sub
    Private Sub MNU_Service_AlterSO(ServiceOrderId As Guid)

        Dim sman As New SvcManager(AppSettings.LoggedUser)
        Dim ServiceOrder As ServiceOrderType = sman.Get_SO_FromSOId(ServiceOrderId)
        Dim form As SVC_ServiceOrder_Form = Me.GetFormInstanceForDataObject(ServiceOrder)
        If form Is Nothing Then
            form = New SVC_ServiceOrder_Form
            form.MdiParent = Me
            form.Show(FormActionOnDataEnum.UpdateRecord, ServiceOrder)
        Else
            form.Activate()
        End If

    End Sub
    Private Sub MNU_Service_Customers()

        SVC_Customers_Form.MdiParent = Me
        SVC_Customers_Form.Show()
        SVC_Customers_Form.Activate()

    End Sub
#End Region

#Region "Catalog"
    Private Sub MNU_Catalog_AddEquipment()
        Dim form As New SQC_CatalogItem_Form
        form.MdiParent = Me
        form.Show(FormActionOnDataEnum.NewRecord)
    End Sub
    Private Sub MNU_Catalog_AlterEquipment(catalogItemTypeObject As CatalogItemType)

        Dim form As SQC_CatalogItem_Form = Me.GetFormInstanceForDataObject(catalogItemTypeObject)
        If form Is Nothing Then
            form = New SQC_CatalogItem_Form
            form.MdiParent = Me
            form.Show(FormActionOnDataEnum.UpdateRecord, catalogItemTypeObject)
        Else
            form.Activate()
        End If

    End Sub
    Private Sub MNU_Catalog_CloneEquipment(catalogItemTypeObject As CatalogItemType)

        Dim form As New SQC_CatalogItem_Form
        form.MdiParent = Me
        catalogItemTypeObject.EquipmentId = Guid.NewGuid
        form.Show(FormActionOnDataEnum.NewRecord, catalogItemTypeObject)

    End Sub
    Private Sub MNU_Catalog_DeleteEquipment(catalogItemTypeObject As CatalogItemType)

        If AMsg.ShowDialog("Are you sure you want to delete the selected " & _
                           "equipment (" & catalogItemTypeObject.ToString & ")," & _
                           " this action cannot be un-done.", MsgBoxStyle.Question) _
                       = Windows.Forms.DialogResult.Yes Then
            Dim catMan As New SqcManager(AppSettings.LoggedUser)
            catMan.Delete_CatalogItem(catalogItemTypeObject)
            Me.OrderRefreshRelatedDataControls(Me, RelatedControlDataCategoriesEnum.EquipmentCatalog, _
                                               catalogItemTypeObject, FormActionOnDataEnum.DeleteRecord)
            Me.StatusInfoMsgPut("Equipment " & catalogItemTypeObject.ToString & " has been sucessfully deleted...")
        End If

    End Sub
    Private Sub MNU_Catalog_Manufacturers()
        SQC_Manufacturers_Form.MdiParent = Me
        SQC_Manufacturers_Form.Show()
        SQC_Manufacturers_Form.Activate()
    End Sub
#End Region

#Region "Spare Parts"
    Private Sub MNU_SpareParts_BulkStockEntry()
        STK_BulkEntry_Form.MdiParent = Me
        STK_BulkEntry_Form.Show()
        STK_BulkEntry_Form.Activate()
    End Sub
    Private Sub MNU_SpareParts_InventoryBatches()
        STK_InventoryBatches_Form.MdiParent = Me
        STK_InventoryBatches_Form.Show()
        STK_InventoryBatches_Form.Activate()
    End Sub
    Private Sub MNU_SpareParts_NewGRN()
        Dim frm As New STK_Grn_Form
        frm.MdiParent = Me
        frm.Show(Nothing, Nothing)
    End Sub
    Private Sub MNU_SpareParts_ConfigureCatalogSpares()
        STK_CatalogItemAssociationToSpares_Form.MdiParent = Me
        STK_CatalogItemAssociationToSpares_Form.Show()
        STK_CatalogItemAssociationToSpares_Form.Activate()
    End Sub
#End Region

#Region "Security"
    Private Sub EnableDisableAllowedAndDisallowedFunctions()
        'SECURITY TAB Functions
        Select Case AppSettings.LoggedUser.AccessLevel
            Case Is = SEC_AccessLevelsEnum.System_Administrator
                Security_AddUser_ImageButton.Enabled = True
                Security_AlterUser_ImageButton.Enabled = True
                Security_DeleteUser_ImageButton.Enabled = True
                Security_ChangePassword_ImageButton.Enabled = True
                Security_ResetAdminUser_ImageButton.Enabled = True
                Security_SetAdminPassword_ImageButton.Enabled = True
            Case Is = SEC_AccessLevelsEnum.Manager
                Security_AddUser_ImageButton.Enabled = True
                Security_AlterUser_ImageButton.Enabled = True
                Security_DeleteUser_ImageButton.Enabled = True
                Security_ChangePassword_ImageButton.Enabled = True
                Security_ResetAdminUser_ImageButton.Enabled = True
                Security_SetAdminPassword_ImageButton.Enabled = False
            Case Is = SEC_AccessLevelsEnum.Stock_Manager
                Security_AddUser_ImageButton.Enabled = False
                Security_AlterUser_ImageButton.Enabled = False
                Security_DeleteUser_ImageButton.Enabled = False
                Security_ChangePassword_ImageButton.Enabled = True
                Security_ResetAdminUser_ImageButton.Enabled = False
                Security_SetAdminPassword_ImageButton.Enabled = False
            Case Is = SEC_AccessLevelsEnum.Technician
                Security_AddUser_ImageButton.Enabled = False
                Security_AlterUser_ImageButton.Enabled = False
                Security_DeleteUser_ImageButton.Enabled = False
                Security_ChangePassword_ImageButton.Enabled = True
                Security_ResetAdminUser_ImageButton.Enabled = False
                Security_SetAdminPassword_ImageButton.Enabled = False
            Case Is = SEC_AccessLevelsEnum.Reception
                Security_AddUser_ImageButton.Enabled = False
                Security_AlterUser_ImageButton.Enabled = False
                Security_DeleteUser_ImageButton.Enabled = False
                Security_ChangePassword_ImageButton.Enabled = True
                Security_ResetAdminUser_ImageButton.Enabled = False
                Security_SetAdminPassword_ImageButton.Enabled = False
        End Select
        'SYSTEM TAB Functions
        Select Case AppSettings.LoggedUser.AccessLevel
            Case Is = SEC_AccessLevelsEnum.System_Administrator
                System_ConfiguewDB_ImageButton.Enabled = True
            Case Is = SEC_AccessLevelsEnum.Manager
                System_ConfiguewDB_ImageButton.Enabled = True
            Case Is = SEC_AccessLevelsEnum.Stock_Manager
                System_ConfiguewDB_ImageButton.Enabled = False
            Case Is = SEC_AccessLevelsEnum.Technician
                System_ConfiguewDB_ImageButton.Enabled = False
            Case Is = SEC_AccessLevelsEnum.Reception
                System_ConfiguewDB_ImageButton.Enabled = False
        End Select
    End Sub
    Private Sub MNU_Security_AddUser()
        Dim form As New SEC_EditUserInfo
        form.MdiParent = Me
        form.Show(FormActionOnDataEnum.NewRecord)
    End Sub
    Private Sub MNU_Security_AlterUser(UserTypeObject As UserType)

        Dim form As SEC_EditUserInfo = Me.GetFormInstanceForDataObject(UserTypeObject)
        If form Is Nothing Then
            form = New SEC_EditUserInfo
            form.MdiParent = Me
            form.Show(FormActionOnDataEnum.UpdateRecord, UserTypeObject)
        Else
            form.Activate()
        End If

    End Sub
    Private Sub MNU_Security_DeleteUser(UserTypeObject As UserType)
        If AMsg.ShowDialog("Are you sure you want to delete the selected " & _
                           "user (" & UserTypeObject.UserName & "), this action cannot " & _
                           "be un-done.", MsgBoxStyle.Question) = Windows.Forms.DialogResult.Yes Then
            Dim secMan As New SecManager(AppSettings.LoggedUser)
            secMan.Delete_UserInfo(UserTypeObject)
            Me.OrderRefreshRelatedDataControls(Me, RelatedControlDataCategoriesEnum.UserInformation, UserTypeObject, FormActionOnDataEnum.DeleteRecord)
            Me.StatusInfoMsgPut("User " & UserTypeObject.UserName & " has been successfully deleted...")
        End If
    End Sub
    Private Sub MNU_Security_ChangePassword()
        Dim setOwnPasswordDialog As New SEC_PasswordSetup_Dialog
        setOwnPasswordDialog.Title = AppSettings.LoggedUser.UserName & " Password Setup"
        If setOwnPasswordDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim secMan As New SecManager(AppSettings.LoggedUser)
            AppSettings.LoggedUser.PasswordVisible = setOwnPasswordDialog.Password
            secMan.Update_UserInfo(AppSettings.LoggedUser)
            setOwnPasswordDialog.Title = AppSettings.LoggedUser.UserName & " Password Setup"
            StatusInfoMsgPut("Your password has been succefully set.. " & _
                             "use the new password upon your next login")
        End If
    End Sub
    Private Sub MNU_Security_ResetAdminUser()

        If AppSettings.LoggedUser.UserType = SEC_UserTypesEnum.System And AppSettings.LoggedUser.UserName = "Admin" Then
            If AMsg.ShowDialog("This will reset the admin user authentication information " & _
                                          "including password, and all rights to its default values. " & _
                                          "Also you are currently logged in as admin user, and hence " & _
                                          "will require an application restart after the reset, so make " & _
                                          "sure you have saved any unsaved work before the reset and restart. " & _
                                          "Are you sure you want to reset the admin user.", _
                                          MsgBoxStyle.Question) = MsgBoxResult.Yes Then
                Dim secMan As New SecManager(AppSettings.LoggedUser)
                secMan.Reset_AdminUser()
                StatusInfoMsgPut("Admin user has been successfully reset...")
                Application.Restart()
            End If
        Else
            If AMsg.ShowDialog("This will reset the admin user authentication information " & _
                              "including password, and all rights to its default values. " & _
                              "Are you sure you want to reset the admin user.", _
                              MsgBoxStyle.Question) = MsgBoxResult.Yes Then
                Dim secMan As New SecManager(AppSettings.LoggedUser)
                secMan.Reset_AdminUser()
                StatusInfoMsgPut("Admin user has been successfully reset...")
            End If
        End If

    End Sub
    Private Sub MNU_Security_SetAdminPassword()
        Dim secMan As New SecManager(AppSettings.LoggedUser)
        Dim adminUser As UserType = secMan.Get_AdminUser
        Dim setAminPasswordDialog As New SEC_PasswordSetup_Dialog
        setAminPasswordDialog.Title = adminUser.UserName & " Password Setup"
        If setAminPasswordDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            secMan.SetPassword_AdminUser(setAminPasswordDialog.Password)
            StatusInfoMsgPut("Admin user password has been succefully set.. " & _
                             "use the new password upon your next login")
        End If
    End Sub
#End Region

#Region "System"
    Private Sub MNU_System_ConfigureDB()
        If AMsg.ShowDialog("After this change an application restart will occur, " & _
                           "so it is recommended that you save any un-saved changes " & _
                           "before you continue. Do you want to continue.", _
                           MsgBoxStyle.Question) = Windows.Forms.DialogResult.Yes Then
            Dim DLG As SQL_Connection_Dialog = New SQL_Connection_Dialog
            DLG.ConnectionString = SysManager.GetConnectionString
            If DLG.ShowDialog() = Windows.Forms.DialogResult.OK Then
                SysManager.SetConnectoinString(DLG.ConnectionString)
                Application.Restart()
            Else
                Application.Exit()
            End If
        End If
    End Sub
    Private Sub MNU_System_DbBackup()

        SYS_DatabaseBackup.MdiParent = Me
        SYS_DatabaseBackup.Show()
        SYS_DatabaseBackup.Activate()

    End Sub
#End Region

#Region "License"
    Private Sub LicenseTabControlsResizeRelocate()
        Banner_License_TabPage.SuspendLayout()
        Dim tW As Integer = Banner_License_TabPage.Size.Width
        Dim tH As Integer = Banner_License_TabPage.Size.Height

        LicenseTab_Logo_PictureBox.Location = New Point(tW - 347 - 8, 16)
        LicenseTab_Logo_PictureBox.Size = New Size(347, tH - 8 - 16)

        LicenseTab_CompanyName_Label.Location = New Point(8, 8)
        LicenseTab_CompanyName_Label.Size = New Size(tW - 8 - LicenseTab_Logo_PictureBox.Size.Width - 8, 18)

        LicenseTab_ComapanyAddress_Label.Location = New Point(15, 26)
        LicenseTab_ComapanyAddress_Label.Size = New Size(tW - 15 - LicenseTab_Logo_PictureBox.Size.Width - 8, tH - 8 - 18 - 8 - 8)
        Banner_License_TabPage.ResumeLayout()
    End Sub
    Private Sub LoadLicenseInformation()

        Dim lic As New LicenseType
        Dim sysMan As New SysManager(AppSettings.LoggedUser)
        sysMan.LicenseInformation_Load(lic)

        LicenseTab_CompanyName_Label.Text = lic.CompanyName
        LicenseTab_ComapanyAddress_Label.Text = lic.Address & vbNewLine & lic.Contacts
        If Not lic.LogoBannerImage Is Nothing Then
            LicenseTab_Logo_PictureBox.Image = lic.LogoBannerImage
        Else
            LicenseTab_Logo_PictureBox.Image = Nothing
        End If

    End Sub
#End Region

#Region "Status Notification"
    Private Sub RefreshLoginInformation()
        StatusBar_LoggedUser_ToolStripStatusLabel.Text = AppSettings.LoggedUser.UserName & " (" & AppSettings.LoggedUser.AccessLevel.ToString & ")"
    End Sub
    Private iStatusMsgStartTime As DateTime = DateTime.MinValue
    Private iStatusMsgShowingDuration As Integer = 2
    Private iStatusMsgs As New Collections.Queue
    Private iStatusMsgForeColorNormal As Color = SystemColors.ControlText
    Private iStatusMsgBackColorNormal As Color = SystemColors.GradientInactiveCaption
    Private iStatusMsgForeColorAnimating As Color = SystemColors.ControlText
    Private iStatusMsgBackColorAnimating As Color = [Color].FromArgb(255, 255, 100)
    Private Sub InitializeStatusInfoMsgBar()
        'Main_StatusStrip.BackColor = Color.FromArgb(127, Main_StatusStrip.BackColor)
        'Main_StatusStrip.BackColor = SystemColors.GradientInactiveCaption
        Main_StatusStrip.BackColor = Me.iStatusMsgBackColorNormal
        Main_StatusStrip.ForeColor = Me.iStatusMsgForeColorNormal
        StatusBar_Info_ToolStripStatusLabel.Text = ""
        StatusInfoMsg_Timer.Stop()
        iStatusMsgStartTime = DateTime.MinValue
    End Sub
    Public Sub StatusInfoMsgPut(Msg As String)
        iStatusMsgs.Enqueue(Msg)
        iStatusMsgStartTime = DateTime.Now
        If StatusInfoMsg_Timer.Enabled = False Then StatusInfoMsg_Timer.Start()
    End Sub
    Private Sub StatusInfoMsg_Timer_Tick(sender As System.Object, e As System.EventArgs) Handles StatusInfoMsg_Timer.Tick
        If StatusBar_Info_ToolStripStatusLabel.Text = "" Then StatusBar_Info_ToolStripStatusLabel.Text = iStatusMsgs.Dequeue
        If Main_StatusStrip.BackColor = Me.iStatusMsgBackColorNormal Then
            Main_StatusStrip.BackColor = Me.iStatusMsgBackColorAnimating
            Main_StatusStrip.ForeColor = Me.iStatusMsgForeColorAnimating
        Else
            Main_StatusStrip.BackColor = Me.iStatusMsgBackColorNormal
            Main_StatusStrip.ForeColor = Me.iStatusMsgForeColorNormal
        End If
        'If Main_StatusStrip.BackColor = SystemColors.GradientInactiveCaption Then
        '    Main_StatusStrip.BackColor = SystemColors.Info
        'Else
        '    Main_StatusStrip.BackColor = SystemColors.GradientInactiveCaption
        'End If
        If iStatusMsgStartTime = DateTime.MinValue Then
            iStatusMsgStartTime = DateTime.Now
        Else
            If (DateDiff(DateInterval.Second, iStatusMsgStartTime, DateTime.Now)) > iStatusMsgShowingDuration Then
                If iStatusMsgs.Count > 0 Then
                    StatusBar_Info_ToolStripStatusLabel.Text = iStatusMsgs.Dequeue
                    iStatusMsgStartTime = DateTime.Now
                Else
                    StatusInfoMsg_Timer.Stop()
                    InitializeStatusInfoMsgBar()
                End If
            End If
        End If
    End Sub

#End Region

#End Region

#Region "ICARF Implementations"
    Public Sub OrderRefreshRelatedDataControls(sender As System.Windows.Forms.Form, _
                                               DataTypeCategory As RelatedControlDataCategoriesEnum, _
                                               Optional DataItem As Object = Nothing, _
                                               Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfParentWindow.OrderRefreshRelatedDataControls

        'First refresh own data
        Select Case DataTypeCategory
            Case Is = RelatedControlDataCategoriesEnum.UserInformation
                If iPanelContentsType = PanelContentTypesEnum.Security Then
                    Me.PanelContentsRefreshExisting()
                End If
            Case Is = RelatedControlDataCategoriesEnum.EquipmentCatalog
                If Me.iPanelContentsType = PanelContentTypesEnum.Catalog Then
                    Me.PanelContentsRefreshExisting()
                End If
        End Select

        'Then callup Refresh for child forms too
        For Each childForm As Form In Me.MdiChildren
            If (TypeOf childForm Is ICarfDataEditingChildWindow) Then
                Dim carfObject As ICarfDataEditingChildWindow = childForm
                carfObject.ProcessRefreshRelatedDataControls(sender, DataTypeCategory, DataItem, ActionOnDataItem)
            End If
            If (TypeOf childForm Is ICarfChildWindow) Then
                Dim carfObject As ICarfChildWindow = childForm
                carfObject.ProcessRefreshRelatedDataControls(sender, DataTypeCategory, DataItem, ActionOnDataItem)
            End If
        Next

    End Sub
    Public Function GetFormInstanceForDataObject(DataObject As Object) As System.Windows.Forms.Form Implements ICarfParentWindow.GetFormInstanceForDataObject
        Dim childForm As Form = Nothing
        For Each f As Form In Me.MdiChildren
            If (TypeOf f Is ICarfChildWindow) Then
                Dim icarfForm As ICarfChildWindow = f
                childForm = icarfForm.GetFormInstanceForDataObject(DataObject)
            ElseIf (TypeOf f Is ICarfDataEditingChildWindow) Then
                Dim icarfForm As ICarfDataEditingChildWindow = f
                childForm = icarfForm.GetFormInstanceForDataObject(DataObject)
            End If
            If childForm IsNot Nothing Then Exit For
        Next
        Return childForm
    End Function
    Public Sub InitializeThisFormAndControlSettings(Optional OrderUpdateToFile As Boolean = False) Implements ICarfParentWindow.InitializeThisFormAndControlSettings
        'init general settings
        AppSettings.InitFormSettings(Me, OrderUpdateToFile)
        If OrderUpdateToFile Then
            'side bar panel width
            AppSettings.PushFormSettingsValue(Me, LeftSideBar_Panel, "Width", LeftSideBar_Panel.Width)

        Else
            'side bar panel width
            If AppSettings.Settings.FormSettings.Item(Me, LeftSideBar_Panel, "Width") IsNot Nothing Then
                Me.LeftSideBar_Panel.Width = AppSettings.Settings.FormSettings.Item(Me, LeftSideBar_Panel, "Width").Value
            End If

        End If
    End Sub
#End Region










End Class
